<?php

namespace SinoPHP\BbdBoss\Controller;

use SinoPHP\BbdBoss\AdminController;

/**
 * 管理员操作日志
 */
class AdminOperateLog extends AdminController {

    /**
     * 管理员操作日志列表
     */
    public function logList() {
        /**
         * 页面请求
         */
        if (!request()->isPost()) {
            $this->setJsResource(array(
                '/plugIn/validate/jquery.validate.min.js',
                '/plugIn/validate/jquery.validate.custom.js',
                '/plugIn/date/datepicker/moment.js',
                '/plugIn/date/datepicker/datepicker.js',
                'modules/log/operateLog.js',
            ));

            $this->setCssResource(array(
                '/plugIn/date/datepicker/datepicker.css',
                'modules/log/operateLog.css',
            ));

            $this->setTitle('操作日志');

            $this->view();
        }

        /**
         * API请求
         */
        $pageSize = request()->post('pageSize') ? : 20;
        $pageNumber = request()->post('pageNumber') ? : 1;
        $username = request()->post('username');
        $action = request()->post('action');
        $ip = request()->post('ip');
        $startTime = request()->post('startTime');
        $endTime = request()->post('endTime');
        $orderBy = request()->post('orderBy');

        // 查询条件
        $where = [];
        if (is_numeric($startTime) && $startTime > 0) {
            $where['create_time|>='] = (int) $startTime;
        }

        if (is_numeric($endTime) && $endTime > 0) {
            $where['create_time|<='] = (int) $endTime;
        }

        if ($username) {
            $where['username'] = $username;
        }

        if ($action) {
            $where['action|~'] = $action;
        }

        if ($ip) {
            if (!filter_var($ip, FILTER_VALIDATE_IP)) {
                $this->error(102006);
            }
            $where['ip'] = $ip;
        }

        // 排序
        if (!is_array($orderBy)) {
            $orderBy = [];
        }
        foreach ($orderBy as $field => $type) {
            if (!in_array($field, ['create_time'])) {
                unset($orderBy[$field]);
            }

            if (!in_array(strtoupper($type), ['DESC', 'ASC'])) {
                unset($orderBy[$field]);
            }
        }

        if (empty($orderBy)) {
            $orderBy = ['log_id' => 'DESC'];
        }

        // 查询总条数&列表
        $totalCount = model('AdminOperateLog')->count($where);
        if ($totalCount > 0) {
            $logList = model('AdminOperateLog')->getLogList($where, $pageNumber, $pageSize, $orderBy);
        } else {
            $logList = [];
        }

        // 格式化时间
        foreach ($logList as $key => $log) {
            $logList[$key]['create_time'] = date('Y-m-d H:i:s', $log['create_time']);
        }

        $this->success([
            'totalCount' => $totalCount,
            'currentPage' => $pageNumber,
            'logList' => $logList,
        ]);
    }

    /**
     * 管理员操作日志详情
     */
    public function logDetail() {
        if (!request()->isPost()) {
            $this->error(101007);
        }

        $logId = request()->post('logId');
        if (empty($logId)) {
            $this->error(101009);
        }

        $logInfo = model('AdminOperateLog')->getRow('*', ['log_id' => (int) $logId]);
        if (empty($logInfo)) {
            $this->error(101008);
        }

        // 反格式化JSON数据
        $detail = json_decode($logInfo['detail'], true);
        if (!json_last_error()) {
            $logInfo['detail'] = $detail;
        }

        $logInfo['create_time'] = date('Y-m-d H:i:s', $logInfo['create_time']);

        $this->success($logInfo);
    }

}
